<template>
  <div class="home-container">
    <h1>欢迎回来, {{ userName }}!</h1>
    <p>您已成功登录系统</p>
    <button @click="handleLogout">退出登录</button>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const userName = ref('')

// 组件挂载时获取用户信息
onMounted(() => {
  const userData = localStorage.getItem('user')
  if (userData) {
    const user = JSON.parse(userData)
    userName.value = user.name
  } else {
    // 如果没有用户信息，重定向到登录页
    router.push({ name: 'Login' })
  }
})

// 退出登录
const handleLogout = () => {
  localStorage.removeItem('user')
  router.push({ name: 'Login' })
}
</script>

<style scoped>
.home-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 30px;
  background-color: #f8f9fa;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

h1 {
  color: #3498db;
  margin-bottom: 20px;
}

p {
  font-size: 18px;
  color: #555;
  margin-bottom: 30px;
}

button {
  padding: 12px 30px;
  background-color: #e74c3c;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #c0392b;
}
</style>